//
//  FKHotBuinessViewController.m
//  WanTao
//
//  Created by 刘文超 on 2019/8/26.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKHotBuinessViewController.h"
#import "FKHotBuinessViewModel.h"
#import "FKCommodityDetailVC.h"
#import "FKPosterVC.h"
@interface FKHotBuinessViewController ()

@property(nonatomic,strong)UITableView *tableview;
@property(nonatomic,strong)FKHotBuinessViewModel *ViewModel;
@property(nonatomic,assign)NSInteger page;

@property(nonatomic,strong)UIButton *ls_button;
@end

@implementation FKHotBuinessViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.navigationItem.title = self.ls_title;
    
    [self bindView];
    __weak typeof(self)weaSelf=self;
    self.page=1;
    [self loadData];
    self.tableview.mj_header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weaSelf.page=1;
        [weaSelf loadData];
    }];
    self.tableview.mj_footer=[MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        weaSelf.page++;
        [weaSelf loadData];
    }];
    
}
-(void)loadData{
    
    NSMutableDictionary *param = @{}.mutableCopy;
    param[@"item_cate_id"] = self.ID;
    param[@"page"] = @(self.page);
    
    [self.ViewModel fk_loadData:param WithResult:^(id  _Nonnull result) {
        
        [self.ls_button setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.ViewModel.buttonImage]]] forState:UIControlStateNormal];
       if (self.ViewModel.refreshArray.count<10) {
            [self.tableview.mj_footer endRefreshingWithNoMoreData];
        }else{
            [self.tableview.mj_footer endRefreshing];
        }
        [self.tableview.mj_header endRefreshing];
        [self.tableview reloadData];
    }];
    
    
}
-(void)bindView
{
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.offset(0);
        make.bottom.offset(-fkOffsetBottom);
    }];
    
    [self.view addSubview:self.ls_button];
    
    
    fkWeakSelf(self);
    self.ViewModel.didItemBlock = ^(NSString * _Nonnull Id) {
        FKCommodityDetailVC *VC = [FKCommodityDetailVC new];
        VC.itemId = Id;
        [weakself.navigationController pushViewController:VC animated:YES];
    };
    
    self.ViewModel.scrollViewBlock = ^(BOOL isScrollViewBottom) {
        if (isScrollViewBottom) {
            [UIView animateWithDuration:0.3 animations:^{
                CGRect rect = weakself.ls_button.frame;
                rect.origin.x = fkScreenW-fk_adjustW(58)-20;
                weakself.ls_button.frame = rect;
                
            }];
        }else{
            [UIView animateWithDuration:0.3 animations:^{
                CGRect rect = weakself.ls_button.frame;
                rect.origin.x = fkScreenW-20;
                weakself.ls_button.frame = rect;
                
            }];
            
        }
    };
    
}
-(UITableView *)tableview
{
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableview.delegate = self.ViewModel;
        _tableview.dataSource = self.ViewModel;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableview];
    }return _tableview;
}
-(FKHotBuinessViewModel *)ViewModel
{
    if (!_ViewModel) {
        _ViewModel = [[FKHotBuinessViewModel alloc]init];
        
    }return _ViewModel;
}
-(UIButton *)ls_button
{
    if (!_ls_button) {
        _ls_button =[[UIButton alloc]initWithFrame:CGRectMake(fkScreenW-fk_adjustW(58)-20, fkScreenH-fkOffsetBottom-fk_adjustW(158), fk_adjustW(58), fk_adjustW(58))];
        [_ls_button fk_viewCornerRadius:fk_adjustW(58)/2 borderWidth:0 borderColor:[UIColor clearColor]];
        [_ls_button addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }return _ls_button;
}
#pragma mark --按钮点击事件
-(void)BtnClick:(UIButton *)button
{
    
    if (self.ViewModel.buttonImage && self.ViewModel.userHeadImage) {
        FKPosterVC *vc = [[FKPosterVC alloc] init];
        vc.imageUrl = self.ViewModel.buttonImage;
        vc.qrcodeUrl = self.ViewModel.userHeadImage;
        [self presentViewController:vc animated:true completion:nil];
    }
    
}
@end
